<?php


namespace app\common\model\mysql;


use think\Model;

class AdminTaxon extends Model
{
    protected $autoWriteTimestamp = true;

    public static function getAttrAllMessage($id)
    {
        if(empty($id)){
            $result = self::field('id,title as name,pid,navid,name as title,status,condition,remarks')->order('navid')->select()->toArray();
        }else{
            $result = self::field('id,title as name,pid,navid,name as title,status,condition,remarks')->where('id',$id)->select()->toArray();
        }
        return $result;
    }

    public function updateRulesState($id, $field, $value)
    {
        $AuthRule = $this->find($id);
        $AuthRule->setAttr($field, $value);
        $AuthRule->save();
        return '更新成功';
    }

    public function saveRules($rules)
    {
        if ('' == $rules['pid'] || '' == $rules['name'] || '' == $rules['title']) {
            $result = ['code' => -1, 'msg' => '缺少必要字段，请检查'];
        } else {
            $row = $this->whereOr(['name' => $rules['name'], 'title' => $rules['title']])->count();
            if ($row) {
                $result = ['code' => -2, 'msg' => '添加失败，名称或控制器已存在'];
            } else {
                $this->create($rules);
                $result = ['code' => 0, 'msg' => '规则[ ' . $rules['title'] . ' ]添加成功'];
            }
        }
        return $result;
    }

    public function delRules($id)
    {
        if (1 === $id) {
            $result = ['code' => -1, 'msg' => '根节点禁止删除'];
        } else {
            $children = $this->where('pid', $id)->count();
            if ($children > 0) {
                $result = ['code' => -2, 'msg' => '此节点存在子规则，需先删除子规则'];
            } else {
                if ($this->where('id', $id)->delete()) {
                    $result = ['code' => 0, 'msg' => '删除成功'];
                } else {
                    $result = ['code' => -3, 'msg' => '规则不存在'];
                }
            }
            return $result;
        }
    }

    public function updateRules($rules)
    {
        if ('' == $rules['id'] || '' == $rules['pid'] || '' == $rules['name'] || '' == $rules['title']) {
            $result = ['code' => -1, 'msg' => '缺少必要字段，请检查'];
        } else {
            $row = $this->where("id <> {$rules['id']} AND (title = '{$rules['title']}' OR name = '{$rules['name']}')")->count();
            if ($row) {
                $result = ['code' => -2, 'msg' => '更新失败，名称或控制器已存在'];
            } else {
                $AuthRule = $this->find($rules['id']);
                $AuthRule->setAttr('name', $rules['name']);
                $AuthRule->setAttr('title', $rules['title']);
                $AuthRule->setAttr('pid', $rules['pid']);
                $AuthRule->setAttr('navid', $rules['navid']);
                $AuthRule->setAttr('condition', $rules['condition']);
                $AuthRule->setAttr('remarks', $rules['remarks']);
                $AuthRule->setAttr('last_ip', $rules['last_ip']);
                $AuthRule->setAttr('last_time', $rules['last_time']);
                $AuthRule->save();
                $result = ['code' => 0, 'msg' => '规则[ ' . $rules['title'] . ' ]更新成功'];
            }
        }
        return $result;
    }


}